<!--
 * @Description: 积分活动
 * @Author: xiao li
 * @Date: 2021-07-03 11:41:05
 * @LastEditTime: 2022-02-14 09:54:22
 * @LastEditors: xiao li
-->
<template>
  <div class="lb-examine-goods">
    <top-nav />
    <div class="page-main">
      <lb-button
        type="primary"
        icon="el-icon-plus"
        @click="$router.push(`/app/integral/edit`)"
        >{{ $t('menu.IntegralAdd') }}</lb-button
      >
      <div class="space-lg"></div>
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="输入查询" prop="goods_name">
            <el-input
              v-model="searchForm.goods_name"
              placeholder="请输入活动名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="atv_status">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.atv_status"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" fixed></el-table-column
        ><el-table-column prop="goods_info" width="300" label="活动信息">
          <template slot-scope="scope">
            <div class="pb-sm">
              <div class="flex-center pt-md">
                <lb-image
                  class="avatar radius-5"
                  :src="scope.row.goods_type === 1 ? scope.row.cover : ''"
                />
                <div
                  class="flex-1 f-caption c-caption ml-md mr-lg"
                  style="width: 180px"
                >
                  <div class="flex-between">
                    <div
                      class="f-paragraph c-title ellipsis"
                      style="line-height: 1.2; margin-bottom: 4px"
                    >
                      {{ scope.row.goods_name }}
                    </div>
                  </div>
                  <div
                    class="flex-between f-caption mt-md"
                    v-if="scope.row.goods_type === 1"
                  >
                    <div class="c-warning">{{ scope.row.goods_price }}</div>
                  </div>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="show_price" label="兑换价" min-width="180">
        </el-table-column>
        <el-table-column prop="type" label="已兑换/剩余" min-width="120">
          <template slot-scope="scope">
            <div>
              {{ `${scope.row.all_have_stock}/${scope.row.total_stock}` }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="user_limit" label="兑换限制">
          <template slot-scope="scope">
            <div>
              {{
                scope.row.goods_type === 1
                  ? `${scope.row.user_limit}件/人`
                  : `${scope.row.user_limit}张/人`
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="有效期" min-width="340">
          <template slot-scope="scope">
            <p>
              {{ scope.row.start_time | handleTime }} ~
              {{ scope.row.end_time | handleTime }}
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <div>{{ statusType[scope.row.atv_status] }}</div>
          </template>
        </el-table-column>
        <el-table-column min-width="160" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="primary"
                @click="$router.push(`/app/integral/edit?id=${scope.row.id}`)"
                >{{ $t('action.edit') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
              <lb-button
                size="mini"
                plain
                type="success"
                @click="toShowDialog(scope.row)"
                >兑换明细</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>

      <el-dialog
        title="兑换明细"
        :visible.sync="showDialog"
        width="1000px"
        center
      >
        <el-table
          :data="dialogForm.goods_info"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          style="width: 1000px"
          ref="multipleTable"
        >
          <el-table-column
            prop="spe_id"
            label="ID"
            width="80"
            fixed
          ></el-table-column>
          <el-table-column
            prop="spe_name"
            label="规格"
            min-width="250"
          ></el-table-column>
          <el-table-column
            prop="goods_stock"
            label="库存"
            v-if="dialogForm.goods_type === 1"
          ></el-table-column>
          <el-table-column
            prop="goods_price"
            label="原价"
            v-if="dialogForm.goods_type === 1"
          >
            <template slot-scope="scope">
              <div>¥{{ scope.row.goods_price }}</div>
            </template>
          </el-table-column>
          <el-table-column
            prop="stock"
            label="可兑换数量"
            width="100"
          ></el-table-column>
          <el-table-column prop="price" label="兑换价" width="200">
            <template slot-scope="scope">
              {{
                scope.row.goods_type == 1 && scope.row.price * 1 > 0
                  ? `${scope.row.integral}积分+${scope.row.price}元`
                  : `${scope.row.integral}积分`
              }}
            </template>
          </el-table-column>
          <el-table-column
            prop="have_stock"
            label="已兑换数量/剩余"
            width="140"
          >
            <template slot-scope="scope">
              {{ `${scope.row.have_stock}/${scope.row.yu_stock}` }}
            </template>
          </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="submitFormInfo">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>

export default {
  data () {
    return {
      statusOptions: [
        {
          label: '全部',
          value: 0
        },
        {
          label: '未开始',
          value: 1
        },
        {
          label: '进行中',
          value: 2
        },
        {
          label: '已过期',
          value: 3
        }
      ],
      statusType: {
        1: '未开始',
        2: '进行中',
        3: '已过期'
      },
      loading: false,
      searchForm: {
        page: 1,
        limit: 10,
        goods_name: '',
        atv_status: 0
      },
      tableData: [],
      total: 0,
      showDialog: false,
      dialogForm: {}
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let { code, data } = await this.$api.integral.integralList(this.searchForm)
      this.loading = false
      if (code !== 200) return
      data.data.map(item => {
        item.total_stock = item.all_stock - item.all_have_stock
      })
      this.tableData = data.data
      this.total = data.total
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, -1)
      }).catch(() => {

      })
    },
    async updateItem (id, status) {
      this.$api.integral.integralUpdate({ id, status }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status === -1) {
            this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
            this.getTableDataList()
          }
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    },
    async toShowDialog (data) {
      data.goods_info.map(item => {
        item.yu_stock = item.stock - item.have_stock
      })
      this.dialogForm = data
      this.showDialog = !this.showDialog
    }
  },
  filters: {
    handleTime (val, type) {
      let time =
        type === 1
          ? moment(val * 1000).format('YYYY-MM-DD')
          : type === 2
            ? moment(val * 1000).format('HH:mm:ss')
            : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-table {
  .table-goods-info {
    width: 280px;
    height: 80px;
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    font-size: 12px;

    .goods-info-r {
      width: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-around;

      p {
        width: 210px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      .price {
        color: red;
      }
    }

    .el-image {
      min-width: 70px;
      height: 70px;
      margin-right: 10px;
    }

    &:last-child {
      margin-bottom: 0;
    }
  }
}
</style>
